package com.dp.temp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;


@Component
@RestController
@RequestMapping("/autoUpload")
public class hd_check_task_history {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    RedisTemplate redisTemplate;



    //将历史task任务放到新表中存储
    @Async
//    @PostConstruct
    public void taskCheckandTask(){
        List<Map<String,Object>> list = jdbcTemplate.queryForList(" select * from hd_count ");

        Long a = Long.parseLong(list.get(0).get("normal").toString()),
                b = Long.parseLong(list.get(0).get("overdue").toString()),
                c = Long.parseLong(jdbcTemplate.queryForList("SELECT COUNT(0) COUNT FROM hd_check WHERE CREATE_DATE < DATE_SUB(NOW(), INTERVAL 30 DAY) AND RESULT = '0'  ").get(0).get("COUNT").toString()),
                d=Long.parseLong(jdbcTemplate.queryForList("SELECT COUNT(0) COUNT FROM hd_check_task a LEFT JOIN hd_check_plain b ON a.pid = b.id WHERE a.CREATE_DATE < DATE_SUB(NOW(),INTERVAL 30 DAY) AND a.STATUS = '0' AND a.endtime < NOW() AND b.WORK_TYPE = '0'").get(0).get("COUNT").toString());

        jdbcTemplate.execute(" update hd_count set normal = '"+(a+c)+"',overdue = '"+(b+d)+"'");
        jdbcTemplate.execute(" INSERT IGNORE INTO hd_check_history(CID,RWID,HAZARD_CODE,RISK_MEASURE_ID,ONAME,RISK_UNIT_NAME,RISK_EVENT_NAME,OIFRISK,DANGER_NAME,DANGER_LEVEL,HAZARD_DANGER_TYPE,DANGER_DESC,DANGER_SRC,RESULT,PHOTO,PHOTOPATH,DANGER_MANAGE_DEADLINE,REGIST_TIME,REGISTRANT,SHNAME,SHID,CNAME,HDTRUE,HDTRUE_REMARK,HDGRADE,IF_TD,DANGER_STATE,DELETED,CREATE_DATE,CREATE_BY,CREATE_BY_ID,UPDATE_DATE,UPDATE_BY,CSOURCE) SELECT a.CID,a.RWID,a.HAZARD_CODE,a.RISK_MEASURE_ID,a.ONAME,a.RISK_UNIT_NAME,a.RISK_EVENT_NAME,a.OIFRISK,a.DANGER_NAME,a.DANGER_LEVEL,a.HAZARD_DANGER_TYPE,a.DANGER_DESC,a.DANGER_SRC,a.RESULT,a.PHOTO,a.PHOTOPATH,a.DANGER_MANAGE_DEADLINE,a.REGIST_TIME,REGISTRANT,a.SHNAME,a.SHID,a.CNAME,a.HDTRUE,a.HDTRUE_REMARK,a.HDGRADE,a.IF_TD,a.DANGER_STATE,a.DELETED,a.CREATE_DATE,a.CREATE_BY,a.CREATE_BY_ID,a.UPDATE_DATE,a.UPDATE_BY,a.CSOURCE FROM hd_check a WHERE a.CREATE_DATE < DATE_SUB(NOW(), INTERVAL 30 DAY) AND a.RESULT = '0' ");
        add();
    }




    public void add(){
        jdbcTemplate.execute(" INSERT IGNORE INTO hd_check_task_history (RWID,PID,OVERDUECOUNT,STATUS,DNAME,PNAME,UNAME,USERID,INVESTIGATION_DATE,CREATE_DATE,endtime,IS_DEFEND) SELECT a.RWID,a.PID,a.OVERDUECOUNT,a.STATUS,a.DNAME,a.PNAME,a.UNAME,a.USERID,a.INVESTIGATION_DATE,a.CREATE_DATE,a.endtime,a.IS_DEFEND FROM hd_check_task a WHERE a.CREATE_DATE < DATE_SUB(NOW(),INTERVAL 30 DAY) AND a.IS_DEFEND = '0' AND (a.STATUS = '1' OR (a.STATUS = '0' AND a.endtime < NOW())) ");

        jdbcTemplate.execute(" DELETE a FROM hd_check_task a WHERE a.CREATE_DATE < DATE_SUB(NOW(),INTERVAL 30 DAY) AND a.IS_DEFEND = '0' AND (a.STATUS = '1' OR (a.STATUS = '0' AND a.endtime < NOW())) ");

        jdbcTemplate.execute(" DELETE FROM hd_check WHERE CREATE_DATE < DATE_SUB(NOW(), INTERVAL 30 DAY) AND RESULT = '0' ");
    }


}
